home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / demos / 20 / tvxsrc / tvx_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-19  |  8.0 KB  |  393 lines

  1. #include "tvx_defs.ic"
  2. #include "tvx_glbl.ic"
  3.  
  4. #define LOCAL static    /* make locals to this module */
  5.  
  6. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  7.  
  8. /* following are some non-standard routines required by TVX */
  9.  
  10. /* =============================>>> STCOPY <<<============================= */
  11.   stcopy(from, i, to, j)
  12.   char from[],to[];
  13.   BUFFINDEX i,*j;
  14.   { /* ## stcopy string, increment j */
  15.  
  16.     BUFFINDEX k1, k2;
  17.  
  18.     k2 = *j;
  19.     for (k1 = i; from[k1] ; )
  20.       {
  21.     to[k2++] = from[k1++];
  22.       }
  23.     to[k2] = 0;
  24.     *j = k2;
  25.   }
  26.  
  27. /* =============================>>> STRCOPY <<<============================= */
  28.   strcopy(from, i, to, j)
  29.   char from[],to[];
  30.   int i,*j;
  31.   { /* ## stcopy string, increment j */
  32.  
  33.     FAST int k1, k2;
  34.  
  35.     k2 = *j;
  36.     for (k1 = i; from[k1] ; )
  37.       {
  38.     to[k2++] = from[k1++];
  39.       }
  40.     to[k2] = 0;
  41.     *j = k2;
  42.   }
  43.  
  44. /* =============================>>> MIN <<<============================= */
  45.   mint(v1,v2)
  46.   int v1,v2;
  47.   {
  48.     return (v1 > v2 ? v2 : v1);
  49.   }
  50.  
  51. /* =============================>>> MAX <<<============================= */
  52.   maxt(v1,v2)
  53.   int v1,v2;
  54.   {
  55.     return (v1 > v2 ? v1 : v2);
  56.   }
  57.  
  58. /*=============================>>> CLOWER  <<<================================*/
  59.   char clower(ch)
  60.   char ch;
  61.   {
  62.     return ((ch >='A' && ch<='Z') ? ch + ' ' : ch);
  63.   }
  64.  
  65. /*=============================>>> CUPPER  <<<================================*/
  66.   char cupper(ch)
  67.   char ch;
  68.   {
  69.     return ((ch >= 'a' && ch <= 'z') ? ch - ' ' : ch);
  70.   }
  71.  
  72. /* =========================>>> LOWER  <<<==============================*/
  73.   lower(str)
  74.   char str[];
  75.   {
  76.     FAST int i;
  77.  
  78.     for (i=0 ; str[i] ; ++i)
  79.     str[i]=clower(str[i]);
  80.  
  81.   }
  82.  
  83. /* ===========================>>> PRINTC <<<============================== */
  84.   printc(chr)
  85.   char chr;
  86.   { /* send one character to the printer */
  87.  
  88. #ifdef MSDOS
  89. #ifdef CII_C
  90.     bdos(5,chr);    /* cp/m, ms-dos version */
  91. #endif
  92. #ifdef MICROSOFT_C
  93.     bdos(5,(int)chr,0);    /* cp/m, ms-dos version */
  94. #endif
  95. #endif
  96.  
  97. #ifdef OSCPM
  98.     bdos(5,chr);    /* cp/m, ms-dos version */
  99. #endif
  100.  
  101. #ifdef GEM_DOS
  102. #ifdef MW_C
  103.     Cprnout((int) chr);    /* gemdos version */
  104. #else
  105.     gemdos(5,(int) chr);    /* gemdos version */
  106. #endif
  107. #endif
  108.   }
  109.  
  110. /*=============================>>> PROMPT <<<================================*/
  111.   prompt(msg)
  112.   char msg[];
  113.   {
  114.     SLOW int i;
  115.     i = strlen(msg);
  116.     ttwtln(msg,i);
  117. #ifdef SCR_BUF
  118.     ttflush();
  119. #endif
  120.   }
  121.  
  122. /*=============================>>> QUIT <<<================================*/
  123.   quit()
  124.   {
  125.    exit(0);
  126.   }
  127.  
  128. /*=============================>>> RINDX  <<<================================*/
  129.   rindx(str, c)
  130.   char c, str[];
  131.   {  /* rindx - find last occurrence character  c  in string  str */
  132.  
  133.     FAST int i,j;
  134.  
  135.     j = -1;
  136.     for (i = 0 ; str[i] != 0; i++)
  137.         if (str[i] == c)
  138.             j = i;
  139.     return (j);
  140.   }
  141.  
  142. /*=============================>>> REMARK <<<================================*/
  143.   remark(msg)
  144.   char msg[];
  145.   {
  146.     prompt(msg);
  147.     ttwt(CR);
  148. #ifdef USELF
  149.     ttwt(LF);
  150. #endif
  151. #ifdef SCR_BUF
  152.     ttflush();
  153. #endif
  154.   }
  155.  
  156. /*=============================>>> UPPER  <<<================================*/
  157.   upper(str)
  158.   char str[];
  159.   {
  160.     static int i;
  161.  
  162.     for (i=0 ; str[i] ; ++i)
  163.     str[i]=cupper(str[i]);
  164.   }
  165.  
  166. /*=============================>>> WTINT  <<<================================*/
  167.   wtint(intg)
  168.   int intg;
  169.   {
  170.     char chrep[14];
  171.     tvitoa(intg,chrep);
  172.     prompt(chrep);
  173.   }
  174.  
  175. /*=============================>>> LREPLY <<<================================*/
  176.   lreply(msg,maxc)
  177.   char msg[];
  178.   int maxc;
  179.   {
  180.     reply(msg,maxc);
  181.     lower(msg);
  182.   }
  183.  
  184. /*=============================>>> UREPLY <<<================================*/
  185.   ureply(msg,maxc)
  186.   char msg[];
  187.   int maxc;
  188.   {
  189.     reply(msg,maxc);
  190.     upper(msg);
  191.   }
  192.  
  193. /*=============================>>> REPLY <<<================================*/
  194.   reply(msg,maxc)
  195.   char msg[];
  196.   int maxc;
  197.   {
  198. #define CBS 8        /* Backspace */
  199. #define CDL1 21        /* ^U */
  200. #define CDL2 24        /* ^X */
  201. #define CABORT 3    /* ^C */
  202. #define CRET 13        /* cr */
  203. #define CESCAPE    27    /* ESC to allow any char to be entered */
  204. #define BKSPC 8
  205.  
  206.     static char ch, rp;
  207.     static int i;
  208.  
  209.     for (i = 0 ; i < maxc ; )    /* i -> next char */
  210.       {
  211.     ch = ttrd_();        /* read the character */
  212.     if (ch == CESCAPE)    /* literal next */
  213.       {
  214.         ch = ttrd_();
  215.         goto ESC_CONT;
  216.        }
  217.     if (ch == CBS)        /* back space */
  218.       {
  219.         if (i > 0)        /* must be something to delete */
  220.           {
  221.         --i;        /* wipe out char */
  222.         ttwt(BKSPC); ttwt(' '); ttwt(BKSPC);
  223.         if (msg[i] < ' ')    /* double echo ^ chrs */
  224.           {
  225.             ttwt(BKSPC); ttwt(' '); ttwt(BKSPC);
  226.           }
  227.           }
  228. #ifdef SCR_BUF
  229.         ttflush();
  230. #endif
  231.       }
  232. #ifdef USE_WIPE
  233.     else if (ch == CDL1 || ch == CDL2)    /* wipe whole line */
  234.       {
  235.         i = 0;        /* set for loop ++ */
  236.         remark("#");
  237.         prompt("Re-enter? ");
  238.       }
  239. #endif
  240.     else if (ch == CABORT && !ins_mode)
  241.       {
  242.         remark("^C");
  243.         prompt("Exit to operating system - are you sure? (y/n) ");
  244.         rp = ttrd_();
  245.         if (rp == 'y' || rp =='Y')
  246.          {
  247.         remark("y");
  248.         reset();            /* need to reset things */
  249.         exit(0);
  250.          }
  251.         remark("n");
  252.         msg[i] = 0;
  253.         prompt("Re-enter? "); prompt(msg);        /* re-echo */
  254.       }
  255.     else if (ch == CRET)        /* ret, so done */
  256.       {
  257.         remark("");
  258.         msg[i] = 0;
  259.         return;
  260.       }
  261.     else
  262.       {
  263. ESC_CONT:
  264.         msg[i++] = ch;
  265.         msg[i] = 0;            /* always 0 terminate */
  266.         if (ch < ' ')
  267.           {
  268.         ch += '@';
  269.         ttwt('^');
  270.           }
  271.         ttwt(ch);            /* echo char */
  272. #ifdef SCR_BUF
  273.         ttflush();
  274. #endif
  275.       }
  276.       } /* end for */
  277.  
  278.     remark("");
  279.   }
  280.  
  281. /* ============================>>> TTRD_   <<<================================ */
  282.   ttrd_()
  283.   {
  284.     SLOW char tc;
  285. #ifdef RD_FROM_CONSOLE_DIRECTLY
  286. #ifdef OSCPM
  287.     while (!(tc = bdos(6,-1)))        /* cp/m implementation */
  288.     ;
  289. #endif
  290.  
  291. #ifdef MSDOS
  292. #ifdef MICROSOFT_C
  293.     tc = bdos(7,0,0) & 0377;        /* ms-dos implementation */
  294. #endif
  295. #ifdef CII_C
  296.     tc = bdos(7,0) & 0377;        /* ms-dos implementation */
  297. #endif
  298. #endif
  299.  
  300. #ifdef GEM_DOS
  301. #ifdef MW_C
  302.     tc = (int) Bconin(2);        /* gemdos implementation */
  303. #else
  304.     tc = (int) bios(2,2);
  305. #endif
  306. #endif
  307.  
  308. #ifdef UNIX
  309.     tc = ttrd();
  310. #endif
  311.  
  312. #else
  313.     gkbd(&tc);            /* this should work */
  314. #endif
  315.  
  316.     return (tc & 0377);
  317.  
  318.   }
  319.  
  320. /*=============================>>> RDINT <<<================================*/
  321.   rdint(val)
  322.   int *val;
  323.   {
  324.     char chrrep[12];
  325.     reply(chrrep,11);
  326.     *val = tvatoi(chrrep);
  327.   }
  328.  
  329. /* =============================>>> ITOA   <<<============================= */
  330.   tvitoa(intg, str)
  331.   int intg;
  332.   char str[];
  333.   {  /* itoa - convert integer  int  to char string in  str */
  334.  
  335.     FAST int i;
  336.     int d, intval, j;
  337.     char k;
  338.     static char digits[] = "0123456789";
  339.  
  340.     intval = intg >= 0L ? intg : (-intg);
  341.     str[0] = '\0';
  342.     i = 0;
  343.     do
  344.       {                /* generate digits */
  345.         i++;
  346.         d = intval % 10L;    /* mod 10 */
  347.         str[i] = digits[d];
  348.         intval = intval / 10L;
  349.       }
  350.     while (intval != 0L);
  351.     if (intg < 0L)
  352.       {                /* then sign */
  353.         str[++i] = '-';
  354.       }
  355.     for (j = 0 ; j < i ; j++ )
  356.       {                /* then reverse */
  357.         k = str[i];
  358.         str[i--] = str[j];
  359.         str[j] = k;
  360.       }
  361.   }
  362.  
  363. /* ------------------------------------------------------------------------- */
  364.  
  365. /* ============================>>> TVATOI   <<<================================ */
  366.   tvatoi(in)
  367.   char in[];
  368.   {  /* atoi - convert string : Ascii machines! */
  369.  
  370.     FAST int i;
  371.     int d, val, neg;
  372.     
  373.     for (i=0 ; in[i] == ' ' || in[i] == '\t' ; i++)
  374.         ;
  375.     if (in[i] == '-')        /* look for negative */
  376.       {
  377.     i++;
  378.     neg=1;
  379.       }
  380.     else
  381.     neg=0;
  382.     for (val = 0; in[i] ; i++)
  383.       {
  384.     if (in[i]<'0' || in[i]>'9')
  385.         break;
  386.     d = in[i]-'0';
  387.         val = 10 * val + d;
  388.       }
  389.     if (neg)
  390.     val = (-val);
  391.     return (val);
  392.   }
  393.